package cn.javabb.bootdemo.service;

import cn.javabb.bootdemo.entity.StPptn;
import cn.javabb.bootdemo.entity.StRiver;
import cn.javabb.bootdemo.entity.StRsvr;
import cn.javabb.bootdemo.mapper.local.StLocalMapper;
import cn.javabb.bootdemo.mapper.oracle.StOracleMapper;
import cn.javabb.bootdemo.mapper.remote.StRemoteMapper;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2019/1/18/018 13:12
 * @Since V1.0
 */
@Slf4j
@Service
public class StDataSynService {
    @Autowired
    StLocalMapper stLocalMapper;
    @Autowired
    StRemoteMapper stRemoteMapper;
    @Autowired
    StOracleMapper stOracleMapper;

    @Autowired
    StOracleMapper stWaterMapper;
    private final static Integer day = 1;


    @Async
    public void pptnSyn(){
        String nowDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        String yesterDay = DateFormatUtils.format(DateUtils.addDays(new Date(),-day),"yyyy-MM-dd HH:mm:ss");
        log.info("[{}]扫描Pptn同步数据....",nowDate);
        //需要同步的数据,往前推一个小时
        List<StPptn> remoteList = stRemoteMapper.listPptnRemote(nowDate,yesterDay);
        int total = 0;
        if(null!=remoteList && remoteList.size()>0){
            total = remoteList.size();
            log.info("获取Pptn数据：{}条。",total);
            Long startTM = System.currentTimeMillis();
            //执行同步
            int n=0;
            for(StPptn pptn:remoteList){
                n += this.insertPptn(pptn);
            }
            if(n>0){
                if(n == total){
                    log.info("Pptn同步成功.用时：{}秒,需要同步记录:{},成功:{}",(System.currentTimeMillis()-startTM)/1000,total,n);
                }else{
                    log.info("Pptn同步成功.用时：{}秒,需要同步记录:{},成功:{},失败:{}",(System.currentTimeMillis()-startTM)/1000,total,n,(total-n));
                }
            }

        }else{
            log.info("Pptn无数据同步");
        }
    }
    @Async
    public void riverSyn(){
        String nowDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        String yesterDay = DateFormatUtils.format(DateUtils.addDays(new Date(),-day),"yyyy-MM-dd HH:mm:ss");
        log.info("[{}]扫描river同步数据....",nowDate);
        //需要同步的数据,往前推一个小时
        List<StRiver> remoteList = stRemoteMapper.listRiverRemote(nowDate,yesterDay);
        int total = 0;
        if(null!=remoteList && remoteList.size()>0){
            total = remoteList.size();
            log.info("获取river数据：{}条。",total);
            Long startTM = System.currentTimeMillis();
            //执行同步
            int n=0;
            for(StRiver river:remoteList){
                n += this.insertRiver(river);
            }
            if(n>0){
                if(n == total){
                    log.info("river同步成功.用时：{}秒,需要同步记录:{},成功:{}",(System.currentTimeMillis()-startTM)/1000,total,n);
                }else{
                    log.info("river同步成功.用时：{}秒,需要同步记录:{},成功:{},失败:{}",(System.currentTimeMillis()-startTM)/1000,total,n,(total-n));
                }
            }

        }else{
            log.info("river无数据同步");
        }
    }
    @Async
    public void rsvrSyn(){
        String nowDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        String yesterDay = DateFormatUtils.format(DateUtils.addDays(new Date(),-day),"yyyy-MM-dd HH:mm:ss");
        log.info("[{}]扫描rsvr同步数据....",nowDate);
        //需要同步的数据,往前推一个小时
        List<StRsvr> remoteList = stRemoteMapper.listRsvrRemote(nowDate,yesterDay);
        int total = 0;
        if(null!=remoteList && remoteList.size()>0){
            total = remoteList.size();
            log.info("获取rsvr数据：{}条。",total);
            Long startTM = System.currentTimeMillis();
            //执行同步
            int n=0;
            for(StRsvr rsvr:remoteList){
                n += this.insertRsvr(rsvr);
            }
            if(n>0){
                if(n == total){
                    log.info("rsvr同步成功.用时：{}秒,需要同步记录:{},成功:{}",(System.currentTimeMillis()-startTM)/1000,total,n);
                }else{
                    log.info("rsvr同步成功.用时：{}秒,需要同步记录:{},成功:{},失败:{}",(System.currentTimeMillis()-startTM)/1000,total,n,(total-n));
                }
            }

        }else{
            log.info("rsvr无数据同步");
        }
    }
    @Async
    public void rsvrOracleSyn(){
        String nowDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        String yesterDay = DateFormatUtils.format(DateUtils.addDays(new Date(),-day),"yyyy-MM-dd HH:mm:ss");
        log.info("[{}]扫描Oracle-rsvr同步数据....",nowDate);
        //需要同步的数据,往前推一个小时
        List<StRsvr> remoteList = stOracleMapper.listOracleRsvr(nowDate,yesterDay);
        int total = 0;
        if(null!=remoteList && remoteList.size()>0){
            total = remoteList.size();
            log.info("获取Oracle-rsvr数据：{}条。",total);
            Long startTM = System.currentTimeMillis();
            //执行同步
            int n=0;
            for(StRsvr rsvr:remoteList){
                n += this.insertRsvr(rsvr);
            }
            if(n>0){
                if(n == total){
                    log.info("Oracle-rsvr同步成功.用时：{}秒,需要同步记录:{},成功:{}",(System.currentTimeMillis()-startTM)/1000,total,n);
                }else{
                    log.info("Oracle-rsvr同步成功.用时：{}秒,需要同步记录:{},成功:{},失败:{}",(System.currentTimeMillis()-startTM)/1000,total,n,(total-n));
                }
            }

        }else{
            log.info("Oracle-rsvr无数据同步");
        }
    }

    /**
     *
     * @param pptn
     * @return
     */
    public int insertPptn(StPptn pptn){

        try{
            stLocalMapper.insertPptnOne(pptn);
            return 1;
        }catch (DataAccessException ex){
            final Throwable cause = ex.getCause();
            if(cause instanceof SQLServerException){
                log.debug("SQLServerException:"+cause.getMessage());
            }else{
                log.error(ex.getMessage());
            }
            return 0;
        }

    }
    /**
     *
     * @param river
     * @return
     */
    public int insertRiver(StRiver river){

        try{
            stLocalMapper.insertRiverOne(river);
            return 1;
        }catch (DataAccessException ex){
            final Throwable cause = ex.getCause();
            if(cause instanceof SQLServerException){
                log.debug("SQLServerException:"+cause.getMessage());
            }else{
                log.error(ex.getMessage());
            }
            return 0;
        }

    }
    /**
     *
     * @param rsvr
     * @return
     */
    public int insertRsvr(StRsvr rsvr){

        try{
            stLocalMapper.insertRsvrOne(rsvr);
            return 1;
        }catch (DataAccessException ex){
            final Throwable cause = ex.getCause();
            if(cause instanceof SQLServerException){
                log.debug("SQLServerException:"+cause.getMessage());
            }else{
                log.error(ex.getMessage());
            }
            return 0;
        }

    }
}
